// APP
if (window.plus) {
    signPageReady();
} else {
    document.addEventListener('plusready', signPageReady, false);
}
// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        signPageReady();
    }
});
// 初始化方法
function signPageReady() {
    var _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    var signPage = function () {
        this.init();
    };
    signPage.prototype = {
        // 初始化页面
        init: function () {
            if (AGENT_CLIENT.APP && os.ios) {
                plus.navigator.setFullscreen(true);
            }
            this.bindEvent();
        },
        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 返回
            $("#back").on("click", function () {
                if (AGENT_CLIENT.APP && os.ios) {
                    plus.navigator.setFullscreen(false);
                }
                if(AGENT_CLIENT.APP){
                	plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
                }else{
                	back();
                }
            });
            // 我要赚钱
            $("#btnearn").on("click", function () {
		        	user = getLocalStorage(STORAGE_MANAGER.user);
		        	if(!user){
		        		redirect("../account/login.html?backurl=earnmoney|sign.html");
		        		return;
		        	}            	
	            $.ajax({
                headers: {
                    "token": _currentloginuser.token
                },
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/" + _currentloginuser.id,
                beforeSend: function(){ $.showWaiting(); }
	            }).done(function (data) {
                setLocalStorage(STORAGE_MANAGER.currentmember, data);
	              var _currentloginmember = getLocalStorage(STORAGE_MANAGER.currentmember);
	              if (!_currentloginmember.is_phone) {
                  $.alert("请先绑定手机号", function (e) {
                      redirect("../account/setphone.html?backurl=earnmoney|sign.html");
                  });
	              } else if(_currentloginmember.is_makemoney){
	              	redirect("../earnmoney/earnmoney.html");
	              }else{
	                  $("#signdiv").fadeIn(200);
	              }                
	            }).always(function(data){
	            	$.closeWaiting();
	            });

            });
            // 关闭
            $("#btnclose").on("click", function () {
                $("#signdiv").fadeOut(200);
            });
            // 同意
            $("#checkbox-1-1").on("change", function () {
                var isActive = $(this).prop("checked");
                if (isActive) {
                    $("#btnsign").addClass("button-active");
                } else {
                    $("#btnsign").removeClass("button-active");
                }
            });

            // 签约
            $("#btnsign").on("click", function () {
                model.sign();
            });
        },
        // 签约
        sign: function () {
            var model = this;
            $.ajax({
                headers: {
                    token: _currentloginuser.token
                },
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/member/sign/mm/" + _currentloginuser.id,
                beforeSend: function () {
                    $.showWaiting();
                }
            }).done(function (res) {
                var currentmember = getLocalStorage(STORAGE_MANAGER.currentmember);
                currentmember.is_makemoney = true;
                setLocalStorage(STORAGE_MANAGER.currentmember, currentmember);
                if (AGENT_CLIENT.APP && os.ios) {
                    plus.navigator.setFullscreen(false);
                }
                $.toast("签约成功");
                setTimeout(function () {
                    // 跳转惠赚钱页面
                    redirect("../earnmoney/earnmoney.html", false, "pop-in", function () {
                        plus.webview.currentWebview().close();
                    });
                }, 1500);
            }).always(function (res) {
                $.closeWaiting();
            }).fail(function () {
                $.alert("签约失败！");
            });
        }
    };
    var _signPage = new signPage();
}